import axios from 'axios'  
import { Notify } from 'vant';
var service = axios.create({
    
}) 

export default {
    get(url, param) {
        return new Promise((cback, reject) => {
            axios.get(url, {            
                params: param        
            },).then(res => { 
                              //axios返回的是一个promise对象 
                var res_code = res.status.toString();
                if (res_code.charAt(0) == 2) {
                    cback(res);   //cback在promise执行器内部
                } else {
                    console.log(res, '异常1')
                }
            }).catch(err => {
                if (!err.response) {
                    console.log('请求异常')
                    // Message({
                    //     showClose: true,
                    //     message: '请求异常',
                    //     type: 'error'
                    // });
                Notify({ type: 'danger', message: '请求异常' });
                    
                } else {
                    reject(err.response); 
                    console.log(err.response, '异常2')
                }
            })

        })
    },
    post(url,param){
        return new Promise((resolve, reject) => {
            axios.post(url, require("qs").stringify(param))
           .then(res => {
               resolve(res);
           })
           .catch(err => {
            if (!err.response) {
                console.log('请求错误')
                // Message({
                //     showClose: true,
                //     message: '请求错误',
                //     type: 'error'
                // });
                Notify({ type: 'danger', message: '请求错误' });
            } else {
                reject(err.response); 
                console.log(err.response, '异常2')
            }
        })
       });
    },
    postjson(url,param){
        return new Promise((resolve, reject) => {
            axios.post(url,param)
           .then(res => {
               resolve(res);
           })
           .catch(err => {
            if (!err.response) {
                console.log('请求错误')
                Notify({ type: 'danger', message: '请求错误' });
                // Message({
                //     showClose: true,
                //     message: '请求错误',
                //     type: 'error'
                // });
            } else {
                reject(err.response); 
                console.log(err.response, '异常2')
            }
        })
       });
    }
} 